﻿<UserControl x:Class="VisualJsonEditor.Controls.JsonEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:converters="clr-namespace:MyToolkit.Converters;assembly=MyToolkit.Extended"
             xmlns:myConverters="clr-namespace:VisualJsonEditor.Converters"
             xmlns:controls="clr-namespace:VisualJsonEditor.Controls"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
             xmlns:ui="clr-namespace:MyToolkit.UI;assembly=MyToolkit.Extended"
             xmlns:localization="clr-namespace:VisualJsonEditor.Localization"
             xmlns:models="clr-namespace:VisualJsonEditor.Models"
             Focusable="False" mc:Ignorable="d" x:Name="Self" d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <converters:VisibilityConverter x:Key="VisibilityConverter" />
        <converters:NotConverter x:Key="NotConverter" />
        <myConverters:NumberConverter x:Key="NumberConverter" />
        <myConverters:IntegerConverter x:Key="IntegerConverter" />
        <myConverters:IndexConverter x:Key="IndexConverter" />
        <myConverters:TimeConverter x:Key="TimeConverter" />
        <myConverters:DateConverter x:Key="DateConverter" />
        <myConverters:DoubleUpDownRangeConverter x:Key="DoubleUpDownRangeConverter" />
        <myConverters:IntegerUpDownRangeConverter x:Key="IntegerUpDownRangeConverter" />
    </UserControl.Resources>

    <ContentPresenter x:Name="Presenter">
        <ContentPresenter.Resources>
            <DataTemplate x:Key="RootTemplate">
                <ItemsControl d:DataContext="{d:DesignInstance Type=models:JsonObjectModel}" 
                              ItemsSource="{Binding Properties}" Focusable="False">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <controls:JsonEditor Data="{Binding}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
            <DataTemplate x:Key="StringTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                             Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="DateTimeTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <xctk:DateTimePicker Margin="0,0,0,8" TextAlignment="Left" AutoCloseCalendar="True"
                                         Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DateConverter}}" />
                    <!--<TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                         Text="{Binding Value, Converter={StaticResource DateConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="DateTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <xctk:DateTimePicker Margin="0,0,0,8" TextAlignment="Left" Format="LongDate" 
                                         TimePickerVisibility="Collapsed" TimeFormat="Custom" AutoCloseCalendar="True"
                                         Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DateConverter}}" />
                    <!--<TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                         Text="{Binding Value, Converter={StaticResource DateConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="TimeTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <xctk:TimePicker Margin="0,0,0,8" HorizontalAlignment="Stretch" 
                                     Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource TimeConverter}}" />
                    <!--<TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                         Text="{Binding Value, Converter={StaticResource TimeConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="NumberTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <xctk:DoubleUpDown Margin="0,0,0,8" TextAlignment="Left"
                                       Minimum="{Binding Schema.Minimum, Converter={StaticResource DoubleUpDownRangeConverter}, ConverterParameter=min}"
                                       Maximum="{Binding Schema.Maximum, Converter={StaticResource DoubleUpDownRangeConverter}, ConverterParameter=max}"
                                       Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NumberConverter}}" />
                    <!--<TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                         Text="{Binding Value, Converter={StaticResource NumberConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="IntegerTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                                           Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <xctk:IntegerUpDown Margin="0,0,0,8" TextAlignment="Left" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                        Minimum="{Binding Schema.Minimum, Converter={StaticResource IntegerUpDownRangeConverter}, ConverterParameter=min}"
                                        Maximum="{Binding Schema.Maximum, Converter={StaticResource IntegerUpDownRangeConverter}, ConverterParameter=max}"
                                        Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntegerConverter}}" />
                    <!--<TextBox Margin="0,0,0,8" ui:UndoRedoCommandManager.DisableUndoRedo="True"
                                         Text="{Binding Value, Converter={StaticResource IntegerConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="EnumTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <TextBlock Text="{Binding Schema.Description}" Margin="0,0,0,4"
                               Visibility="{Binding Schema.Description, Converter={StaticResource VisibilityConverter}}" />
                    <ComboBox Margin="0,0,0,8" ItemsSource="{Binding Schema.Enumeration}"
                              SelectedItem="{Binding Value, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="BooleanTemplate">
                <StackPanel d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,0,0,4" />
                    <CheckBox Content="{Binding Schema.Description}" IsThreeState="False"
                              IsChecked="{Binding Value, Mode=TwoWay, FallbackValue=false}" Margin="0,0,0,8" />
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="ObjectTemplate">
                <controls:ExpandingGroupBox d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}"
                                            Header="{Binding}" Margin="0,0,0,8" MinHeight="36" IsExpanded="False"
                                            BorderBrush="Black" Background="#0F000000">
                    <controls:ExpandingGroupBox.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Run Text="{Binding Name, Mode=OneWay}" FontWeight="Bold" />
                                <Run Text="{Binding Schema.Description}" /><Run Text=" (object) " />
                            </TextBlock>
                        </DataTemplate>
                    </controls:ExpandingGroupBox.HeaderTemplate>
                    <StackPanel>
                        <CheckBox IsChecked="{Binding HasValue, Mode=OneWay}" 
                                  Visibility="{Binding IsRequired, Converter={StaticResource NotConverter}}"
                                  Margin="4,8,4,4" Content="Specify values" 
                                  Checked="OnCreateObject" Unchecked="OnRemoveObject" Tag="{Binding}" />

                        <controls:JsonEditor Visibility="{Binding HasValue, Converter={StaticResource VisibilityConverter}}" 
                                             Data="{Binding Value}" Margin="4" />
                    </StackPanel>
                </controls:ExpandingGroupBox>
            </DataTemplate>
            <DataTemplate x:Key="ArrayTemplate">
                <controls:ExpandingGroupBox d:DataContext="{d:DesignInstance Type=models:JsonPropertyModel}"
                                            Header="{Binding}" Margin="0,0,0,8" MinHeight="36" IsExpanded="False"
                                            BorderBrush="Black" Background="#0F00FF00">
                    <controls:ExpandingGroupBox.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Run Text="{Binding Name, Mode=OneWay}" FontWeight="Bold" />
                                <Run Text="{Binding Schema.Description}" /><Run Text=" (array)" />
                            </TextBlock>
                        </DataTemplate>
                    </controls:ExpandingGroupBox.HeaderTemplate>
                    <StackPanel Margin="0,8,0,0">
                        <CheckBox IsChecked="{Binding HasValue, Mode=OneWay}" 
                                  Visibility="{Binding IsRequired, Converter={StaticResource NotConverter}}"
                                  Margin="4,0,4,4" Content="Specify values" 
                                  Checked="OnCreateArray" Unchecked="OnRemoveArray" Tag="{Binding}" />

                        <StackPanel Visibility="{Binding HasValue, Converter={StaticResource VisibilityConverter}}">
                            <ItemsControl ItemsSource="{Binding Value}" Focusable="False">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Margin="4,0,4,0">
                                            <controls:ExpandingGroupBox BorderBrush="Black" Margin="0,0,0,8" 
                                                                        Background="#0F000000" MinHeight="36">
                                                <controls:ExpandingGroupBox.Header>
                                                    <MultiBinding Converter="{StaticResource IndexConverter}">
                                                        <Binding />
                                                        <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ItemsControl}" />
                                                    </MultiBinding>
                                                </controls:ExpandingGroupBox.Header>
                                                <controls:ExpandingGroupBox.HeaderTemplate>
                                                    <DataTemplate>
                                                        <TextBlock>
                                                            <Run Text="{Binding Mode=OneWay}" FontWeight="Bold" /><Run Text=" (object)" />
                                                        </TextBlock>
                                                    </DataTemplate>
                                                </controls:ExpandingGroupBox.HeaderTemplate>
                                                <controls:JsonEditor Data="{Binding}" Margin="4,8,4,0" />
                                            </controls:ExpandingGroupBox>

                                            <Button Content="{x:Static localization:Strings.ButtonRemove}" HorizontalAlignment="Right" 
                                                    Padding="8,4,8,4" VerticalAlignment="Top"
                                                    Click="OnRemoveArrayObject" Tag="{Binding}" Margin="4,0,12,4" />
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                            <Button Content="{x:Static localization:Strings.ButtonAdd}" HorizontalAlignment="Left" Padding="8,4,8,4" 
                                    Click="OnAddArrayObject" Tag="{Binding}" Margin="4,8,4,4" />
                        </StackPanel>
                    </StackPanel>
                </controls:ExpandingGroupBox>
            </DataTemplate>
        </ContentPresenter.Resources>
        <ContentPresenter.ContentTemplateSelector>
            <controls:JsonObjectTypeTemplateSelector />
        </ContentPresenter.ContentTemplateSelector>
    </ContentPresenter>
</UserControl>
